package com.twu.biblioteca;

import com.twu.biblioteca.controller.BookController;
import com.twu.biblioteca.controller.MembershipController;
import com.twu.biblioteca.controller.MovieController;
import com.twu.biblioteca.library.BookLibrary;
import com.twu.biblioteca.library.MovieLibrary;
import com.twu.biblioteca.menuitem.MenuItem;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.HashMap;


public class BibliotecaApp {

    public static final int NUMBER_OF_BOOKS = 10;
    public static final int NUMBER_OF_MOVIES = 15;
    public static final int NUMBER_OF_USERS = 5;
    private Security security;
    private Menu menu;
    private PrintStream printStream;
    private BookLibrary bookLibrary;
    private MovieLibrary movieLibrary;

    public BibliotecaApp(PrintStream printStream, BookLibrary bookLibrary, MovieLibrary movieLibrary, Menu menu, Security security) {

        this.printStream = printStream;
        this.bookLibrary = bookLibrary;
        this.bookLibrary.setPrintStream(printStream);
        this.movieLibrary = movieLibrary;
        this.movieLibrary.setPrintStream(printStream);
        this.menu = menu;
        this.menu.setBibliotecaApp(this);
        this.menu.setPrintStream(printStream);
        this.security = security;
    }

    public static void main(String[] args) throws IOException {
        PrintStream printStream = new PrintStream(System.out);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        Security security = new Security(bufferedReader, printStream, new Initializer().initializeUsers(NUMBER_OF_USERS));
        BookLibrary bookLibrary = new BookLibrary(new Initializer().intiliazeBooks(NUMBER_OF_BOOKS));
        MovieLibrary movieLibrary = new MovieLibrary(new Initializer().intiliazeMovies(NUMBER_OF_MOVIES));
        BookController bookController = new BookController(security, bookLibrary, printStream);
        MovieController movieController = new MovieController(security, movieLibrary, printStream);
        MembershipController membershipController = new MembershipController(security,null, printStream);
        HashMap<Integer, MenuItem> menuItemHashMap = new Initializer().initializeMenuItems(bookController, movieController, membershipController);
        Menu menu = new Menu(bufferedReader, menuItemHashMap);
        bookController.setMenu(menu);

        BibliotecaApp app = new BibliotecaApp(printStream, bookLibrary, movieLibrary, menu, security);

        app.start();
    }

    public void start() {
        int option;
        menu.welcome();

        do {
            menu.showMenuOptions();
            option = menu.readMenuOption();
            if (option != 0) {
                menu.processMenuOption(option);
            }
        } while (option != 0 );
    }

}
